package org.ami.onlineschool.ui;

import java.awt.EventQueue;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.GridLayout;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JList;

import org.ami.onlineschool.dataAccess.RunningCoursesDataAccess;
import org.ami.onlineschool.dataAccess.SchoolDataAccess;
import org.ami.onlineschool.model.Assignment;
import org.ami.onlineschool.model.Course;
import org.ami.onlineschool.model.Semester;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class UIMonitorCourseExecution extends AbstractWorkWindow{

	private JFrame frame;
	private JTextField txtAbc;
	private SchoolDataAccess schoolDataAccess;
	private RunningCoursesDataAccess runningCoursesDataAccess;
	private JList lstAvailableCourses;
	private JList lstRunningCourses;
	private JList lstTodaysAssignments;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		launchWindow(new File("").getAbsolutePath());
	}

	static void launchWindow(final String dataLocation) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					UIMonitorCourseExecution window = new UIMonitorCourseExecution();
					window.setSchoolDataLocation(dataLocation);
					window.initialize();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public UIMonitorCourseExecution() {
		//initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				try {
					runningCoursesDataAccess.saveRunningCoursesToDisk();
				} catch (FileNotFoundException e1) {
					e1.printStackTrace();
				}
			}
		});
		frame.setBounds(100, 100, 609, 492);
		
		JPanel panel = new JPanel();
		frame.getContentPane().add(panel, BorderLayout.NORTH);
		panel.setLayout(new BorderLayout(0, 0));
		
		txtAbc = new JTextField();
		txtAbc.setText(getSchoolDataLocation());
		panel.add(txtAbc);
		txtAbc.setColumns(10);
		
		JButton btnSave = new JButton("Save");
		panel.add(btnSave, BorderLayout.EAST);
		
		JPanel panel_1 = new JPanel();
		frame.getContentPane().add(panel_1, BorderLayout.CENTER);
		panel_1.setLayout(new GridLayout(0, 3, 0, 0));
		
		JPanel panel_2 = new JPanel();
		panel_1.add(panel_2);
		panel_2.setLayout(new BorderLayout(0, 0));
		
		JPanel panel_5 = new JPanel();
		panel_2.add(panel_5, BorderLayout.NORTH);
		
		JLabel lblAvailableCourses = new JLabel("Available Courses");
		panel_5.add(lblAvailableCourses);
		
		JButton btnStartCourse = new JButton("Start");
		btnStartCourse.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Course selectedCourse = (Course) lstAvailableCourses.getSelectedValue();
				if(selectedCourse != null) {
					runningCoursesDataAccess.getRunningCourses().add(selectedCourse.getId());
					UIUtilities.getListModel(lstRunningCourses).addElement(selectedCourse);
					UIUtilities.getListModel(lstAvailableCourses).removeElement(selectedCourse);
				}
			}
		});
		panel_5.add(btnStartCourse);
		
		JScrollPane scrollPane = new JScrollPane();
		panel_2.add(scrollPane, BorderLayout.CENTER);
		
		lstAvailableCourses = new JList();
		scrollPane.setViewportView(lstAvailableCourses);
		
		JPanel panel_3 = new JPanel();
		panel_1.add(panel_3);
		panel_3.setLayout(new BorderLayout(0, 0));
		
		JPanel panel_6 = new JPanel();
		panel_3.add(panel_6, BorderLayout.NORTH);
		
		JLabel lblRunningCourses = new JLabel("Running Courses");
		panel_6.add(lblRunningCourses);
		
		JScrollPane scrollPane_1 = new JScrollPane();
		panel_3.add(scrollPane_1, BorderLayout.CENTER);
		
		lstRunningCourses = new JList();
		scrollPane_1.setViewportView(lstRunningCourses);
		
		JPanel panel_4 = new JPanel();
		panel_1.add(panel_4);
		panel_4.setLayout(new BorderLayout(0, 0));
		
		JPanel panel_7 = new JPanel();
		panel_4.add(panel_7, BorderLayout.NORTH);
		
		JLabel lblTodaysAssignments = new JLabel("Today's Assignments");
		panel_7.add(lblTodaysAssignments);
		
		JButton btnSendAllAssignments = new JButton("Send All");
		panel_7.add(btnSendAllAssignments);
		
		JScrollPane scrollPane_2 = new JScrollPane();
		panel_4.add(scrollPane_2, BorderLayout.CENTER);
		
		lstTodaysAssignments = new JList();
		scrollPane_2.setViewportView(lstTodaysAssignments);
		
		schoolDataAccess = new SchoolDataAccess(getSchoolDataLocation());
		runningCoursesDataAccess = new RunningCoursesDataAccess(getSchoolDataLocation());
		
		loadUI();
	}

	private void loadUI() {
		List<String> runningCoursesIds = runningCoursesDataAccess.getRunningCourses();
		List<Course> runningCourses = schoolDataAccess.getSchool().getCourses(runningCoursesIds);
		UIUtilities.populateModelFromList(UIUtilities.getListModel(lstRunningCourses), runningCourses);
		
		List<Course> courses = new ArrayList<Course>(schoolDataAccess.getSchool().getCourses());
		courses.removeAll(runningCourses);
		UIUtilities.populateModelFromList(UIUtilities.getListModel(lstAvailableCourses), courses);
		
		List<Assignment> todaysAssignments = getAssignmentsForDate(new Date(), runningCourses);
		UIUtilities.populateModelFromList(UIUtilities.getListModel(lstTodaysAssignments), todaysAssignments);
		
	}

	private List<Assignment> getAssignmentsForDate(Date date, List<Course> runningCourses) {
		List<Assignment> assignmentsForToday = new ArrayList<Assignment>();
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int day = calendar.get(Calendar.DAY_OF_MONTH);
		int month = calendar.get(Calendar.MONTH) + 1;
		
		for (Course course : runningCourses) {
			List<Semester> semesters = course.getSemesters();
			for (Semester semester : semesters) {
				List<Assignment> assignments = semester.getAssignments();
				for (Assignment assignment : assignments) {
					
					if(assignment.getMonth() == month && assignment.getDay() == day) {
						assignmentsForToday.add(assignment);
					}
				}
			}
		}
		
		return assignmentsForToday;
	}

}
